#include "pathplanmw.h"
#include "ui_pathplanmw.h"
#include <QDebug>

void sleep(unsigned int msec)
{
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

PathplanMW::PathplanMW(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::PathplanMW)
{
    ui->setupUi(this);

    // 设置按钮图标
//    ui->btn_pathplanning->setIcon(QIcon(":/pic/navigation.png"));
//    ui->btn_back->setIcon(QIcon(":/pic/back.png"));

    //路径规划 dijkstra算法
    this->shortes=new pathshow;
    for(int i=0;i<9;i++)
    {
        for(int j=0;j<9;j++)
        {
            colors[i][j]="lightGray";
        }
    }

    mpb[0]= ui->btn_shopping;
    mpb[1]= ui->btn_practice;
    mpb[2]=ui->btn_gymnasium;
    mpb[3]=ui->btn_5teaching;
    mpb[4]=ui->btn_lits;
    mpb[5]=ui->btn_library;
    mpb[6]=ui->btn_3teaching;
    mpb[7]=ui->btn_integrated;
    mpb[8]=ui->btn_art;
}


PathplanMW::~PathplanMW()
{
    delete shortes;
    for (int i = 0; i < 9; ++i) {
            delete mpb[i]; // 逐个释放每个QRadioButton对象
        }
    delete ui;
}

//返回
void PathplanMW::on_btn_back_clicked()
{
    //所以可以利用此函数获取当前窗口的父窗口对象指针
    QWidget *parent = this->parentWidget();
    parent->show();//把之前父界面的隐藏状态改为显示状态，显示父界面
    this->close();//关闭当前界面
}

//路径图样初始化
void PathplanMW::init(){

    for(auto x:this->shortes->List){
        int nw=x.first,nx=x.second.first,dis=x.second.second;
        QPen pen;
        QPainter painter(this);
        pen.setWidth(5);
        pen.setColor(colors[nw][nx]);
        painter.setRenderHint(QPainter::Antialiasing, true); // 反走样
        painter.setPen(pen);
        painter.drawLine(QPointF(mpb[nw]->x()+7,mpb[nw]->y()+7), QPointF(mpb[nx]->x()+7, mpb[nx]->y()+7));// 绘制直线

        pen.setColor("Purple");
        painter.setPen(pen);
        painter.drawText(QPointF((mpb[nw]->x()+mpb[nx]->x())/2,(mpb[nw]->y()+mpb[nx]->y())/2),QString::number(dis));
    }
}

//重写绘画事件
void PathplanMW::paintEvent(QPaintEvent *)
{
    QPainter pbk(this);
    init();
}

//路径规划按钮
void PathplanMW::on_btn_pathplanning_clicked()
{
    if(this->shortes->starcity == ""||this->shortes->overcity== "") return;
    this->c=0;

    for(int i=0;i<12;i++)
    {
        for(int j=0;j<12;j++)
        {
            colors[i][j]="lightGray";
        }
    }
    update();

    int dis[12],n=9,pre[12];
    bool st[12];
    memset(st,false,sizeof (st));
    memset(dis,0x3f,sizeof (dis));
    memset(pre,0x3f,sizeof (pre));
    dis[this->shortes->st]=0;

    for(int i=0;i<n;i++){
        int t=-1;
        for(int j=0;j<n;j++){
            if(!st[j]&&(t==-1||dis[t]>dis[j]))
                t=j;
        }
        st[t]=true;
        if(pre[t]!=0x3f3f3f3f) colors[t][pre[t]]=colors[pre[t]][t]="lightBlue";
        update();
        sleep(200);

        for(auto x:this->shortes->List){
            int nw=x.first,j=x.second.first,tt=x.second.second;
            if(j==t){
               j^=nw;nw^=j;j^=nw;
            }
            if(nw==t){
                if(dis[j]>=dis[t]+tt){
                    dis[j]=dis[t]+tt;
                    pre[j]=t;
                }
            }
        }
        if(t==this->shortes->ed) break;
    }
    int zd =this->shortes->ed,qd=this->shortes->st;
    while(zd!=qd){
        colors[zd][pre[zd]]=colors[pre[zd]][zd]="Blue";
        zd=pre[zd];
    }
    update();
    ui->label_result->setText("最短距离是："+QString::number(dis[this->shortes->ed]));
}

//每个RadioButton按钮
void PathplanMW::place_onclick(QRadioButton *btn)
{

    int v;
    for(int i=0;i<=8;i++){
        if(mpb[i]==btn){
            v=i;
            break;
        }
    }
    if(this->shortes->starcity==""){
        for(int i=0;i<12;i++)
        {
            for(int j=0;j<12;j++)
            {
                colors[i][j]="lightGray";
            }
        }
        update();
        this->ui->label_stpoint->setText(btn->text());
        this->shortes->starcity=btn->text();
        this->ui->label_ovpoint->setText("");
        this->ui->label_result->setText("");
        this->shortes->st=v;
    }else if(this->shortes->starcity==btn->text()||this->shortes->overcity==btn->text()){
        this->ui->label_stpoint->setText("");
        this->shortes->starcity="";

        this->ui->label_ovpoint->setText("");
        this->shortes->overcity="";

        this->ui->label_result->setText("");
    }else{
        for(int i=0;i<12;i++)
        {
            for(int j=0;j<12;j++)
            {
                colors[i][j]="lightGray";
            }
        }
        update();
        this->ui->label_ovpoint->setText(btn->text());
        this->shortes->overcity=btn->text();
        this->ui->label_result->setText("");
        this->shortes->ed=v;
    }
    btn->setAutoExclusive(false);
    btn->setChecked(false);

}
void PathplanMW::on_btn_shopping_clicked()
{
    place_onclick(ui->btn_shopping);
}

void PathplanMW::on_btn_lits_clicked()
{
    place_onclick(ui->btn_lits);
}

void PathplanMW::on_btn_integrated_clicked()
{
    place_onclick(ui->btn_integrated);
}

void PathplanMW::on_btn_practice_clicked()
{
    place_onclick(ui->btn_practice);
}

void PathplanMW::on_btn_gymnasium_clicked()
{
    place_onclick(ui->btn_gymnasium);
}

void PathplanMW::on_btn_art_clicked()
{
    place_onclick(ui->btn_art);
}

void PathplanMW::on_btn_5teaching_clicked()
{
    place_onclick(ui->btn_5teaching);
}

void PathplanMW::on_btn_3teaching_clicked()
{
    place_onclick(ui->btn_3teaching);
}

void PathplanMW::on_btn_library_clicked()
{
    place_onclick(ui->btn_library);
}

